package com.yunhe.webmvc.servlet;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * Created by Administrator on 2017/10/30.
 */
@MultipartConfig(maxFileSize = 1024*1024*20)
@WebServlet(name="Storge", urlPatterns="/upload")
public class UpdateloadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Map<String,String>>  srcList = new ArrayList<>();
        Collection<Part> parts = req.getParts();
        for(Part part : parts) {
            String submittedFileName = part.getSubmittedFileName();
            String suffix = submittedFileName.substring(submittedFileName.lastIndexOf("."));
            String fileName = System.currentTimeMillis() + suffix ;
            part.write("E:\\fs\\" + fileName);
            Map<String,String> map = new HashMap<>();
            map.put("src", fileName);
            srcList.add(map);
        }
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(srcList));
        writer.close();
    }
}
